package com.bestvike.stone.spring.client;

import com.bestvike.stone.core.collection.CollectionUtils;
import com.bestvike.stone.core.collection.ListUtils;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.HttpHeaders;

import java.util.List;

/**
 * Created by 许崇雷 on 2017-11-06.
 */
@Data
@ConfigurationProperties(prefix = "spring.rest")
public final class RestProperties {
    private static final String[] SYSTEM_IGNORE_HEADERS = {HttpHeaders.ACCEPT, HttpHeaders.ACCEPT_ENCODING, HttpHeaders.ACCEPT_LANGUAGE, HttpHeaders.CONTENT_TYPE, HttpHeaders.CONTENT_LENGTH, HttpHeaders.COOKIE, HttpHeaders.SET_COOKIE, HttpHeaders.AUTHORIZATION, HttpHeaders.CONNECTION, HttpHeaders.HOST, HttpHeaders.USER_AGENT};
    private Integer connectTimeout;
    private Integer readTimeout;
    private Boolean skipSslVerification;
    private Boolean routeHeadersEnabled;
    private List<String> ignoreHeaders;

    public boolean isSkipSslVerification() {
        return this.skipSslVerification != null && this.skipSslVerification;
    }

    @SuppressWarnings("unchecked")
    public void config(RestTemplateEx restTemplate) {
        restTemplate.routeHeadersEnabled = this.routeHeadersEnabled != null && this.routeHeadersEnabled;
        HttpHeaders set = new HttpHeaders();
        for (String headerName : SYSTEM_IGNORE_HEADERS)
            set.put(headerName, ListUtils.EMPTY_LIST);
        if (!CollectionUtils.isEmpty(this.ignoreHeaders))
            for (String headerName : this.ignoreHeaders)
                set.put(headerName, ListUtils.EMPTY_LIST);
        restTemplate.ignoreHeaders = set;
    }
}
